/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.storage;

import org.apache.log4j.Logger;

import java.util.*;

public class SubscriptionMap
{
       private static final Logger logger = Logger.getLogger (SubscriptionMap.class);
       private static Map<SubscriberIdentifier,Subscriber> subMap=new HashMap<SubscriberIdentifier,Subscriber>();
      // private static ArrayList<Subscriber> filteredSubscribers=new ArrayList<Subscriber>();
       public static boolean saveSubscriber(SubscriberIdentifier subIdentifier,Subscriber subscriber)
       {
        boolean savedSuccessfully=false;
        if(subMap.isEmpty())
        {
             subMap.put(subIdentifier,subscriber);
             logger.info("Subscriber: "+subIdentifier.getClientID()+" was added to list.");
             savedSuccessfully=true;
             return savedSuccessfully;
        }
        else
        {
          boolean alreadySubscribed=false;
          for(Map.Entry<SubscriberIdentifier,Subscriber> entry:subMap.entrySet())
          {
               Subscriber currentListSubscriber=entry.getValue();
               String currentListSubscriberID=currentListSubscriber.getSubscriberIdentifier().getClientID();
               String currentListSubscriberFilter=currentListSubscriber.getSubscriberIdentifier().getFilterTopic();
               if(((currentListSubscriberID.equals(subIdentifier.getClientID())))&&(currentListSubscriberFilter.equals(subIdentifier.getFilterTopic())))
               {
                   alreadySubscribed=true;
                   logger.info("Subscriber: "+subIdentifier.getClientID()+":"+subIdentifier.getFilterTopic()+" was already in the list.");
                   savedSuccessfully=false;
                   break;
                   /*subMap.put(subIdentifier,subscriber);
                   logger.info("Subscriber: "+subIdentifier.getClientID()+" was added to list.");
                   savedSuccessfully=true; */
               }

            }
            if(!alreadySubscribed)
              {
                  subMap.put(subIdentifier,subscriber);
                  logger.info("Subscriber: "+subIdentifier.getClientID()+" was added to list.");
                  savedSuccessfully=true;
              }

            return savedSuccessfully;
        }

       }

       public static ArrayList<Subscriber> getSubscribersFilteredByTopic(String topic)
       {
            ArrayList<Subscriber> filteredSubscribersList =new ArrayList<Subscriber>();
            String[] topicSplitArray = topic.split("#");
            ArrayList<String> filters = new ArrayList<String>();
            String temp = topicSplitArray[0];

            for(int i = 0; i < topicSplitArray.length; i++)
            {
                if(i == 0)
                    filters.add(temp);
                else
                {
                    temp = temp.concat("#"+topicSplitArray[i]);
                    filters.add(temp);
                }
            }

           for(String filter: filters)
           {
               logger.info(filter);
                for(Map.Entry<SubscriberIdentifier,Subscriber> entry:subMap.entrySet())
                {
                    if(filter.equals(entry.getKey().getFilterTopic()))
                    {
                        Subscriber matchingSubscriber=entry.getValue();
                        matchingSubscriber.setSubscriberIdentifier(entry.getKey());
                        filteredSubscribersList.add(matchingSubscriber);
                    }
                }
           }
           return filteredSubscribersList;
       }

       public static Map<SubscriberIdentifier,Subscriber> getAllSubscribers()
       {
              return subMap;
       }

       public static void deleteSubscriber(String subscriberUUID)
       {
          for(Map.Entry<SubscriberIdentifier,Subscriber> entry:subMap.entrySet())
          {
               if(subscriberUUID.equals(entry.getValue().getUuid()))
               {
                   subMap.remove(entry.getKey());
               }
          }

      }
}
